package com.rich.widget

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.TypedValue
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.appcompat.widget.AppCompatTextView
import com.rich.widget.builder.TextColorBuilder
import com.rich.widget.styleable.ShapeTextViewStyleable

/**
 * 1,描边效果.
 * 2,中划线效果.
 * 3,高仿余额宝数字动画效果.
 */
class RichTextView : AppCompatTextView {

    companion object {

        private const val TAG = "RichTextView"

        //显示表示式
        //const val INT_REGEX = "%1$01" //不保留小数，整数
        const val INT_REGEX = "%1$01.0f" //不保留小数，整数
        const val FLOAT_REGEX = "%1$01.2f" //保留2位小数
    }

    private val linePaint by lazy {
        Paint()
    }

    //划线宽度
    private var lineWidth = 0F

    //划线颜色
    private var lineColor = 0

    //动画时长
    private val duration = 1500L

    /**
     * 获取当前数字,显示数字
     * 根据正则表达式，显示对应数字样式
     */
    private var fNumber = 0F
        set(number) {
            field = number
            text = String.format(regex, number)
        }

    private var iNumber = 0
        set(number) {
            field = number
            text = String.format(regex, number)
        }


    //显示表达式
    private var regex: String = INT_REGEX

    private val mTextStyleable = ShapeTextViewStyleable()

    private lateinit var mTextColorBuilder: TextColorBuilder

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        onInitView(attrs)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawLine(canvas)
    }

    override fun setTextColor(color: Int) {
        super.setTextColor(color)
        if (::mTextColorBuilder.isInitialized) {
            mTextColorBuilder.textColor = color
            mTextColorBuilder.clearTextGradientColors()
        }
    }

    override fun setText(text: CharSequence, type: BufferType) {
        var content: CharSequence = text
        if (::mTextColorBuilder.isInitialized) {
            if ((mTextColorBuilder.isTextGradientColors() || mTextColorBuilder.hasTextStrokeColor())) {
                content = mTextColorBuilder.buildTextSpannable(text)
            }
        }
        super.setText(content, type)
    }

    private fun onInitView(attrs: AttributeSet?) {
        context.obtainStyledAttributes(attrs, R.styleable.RichTextView).let {
            //获取具体属性值
            lineColor = it.getColor(
                R.styleable.RichTextView_lineColor,
                textColors.defaultColor
            )
            lineWidth = it.getDimension(
                R.styleable.RichTextView_lineWidth,
                TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    0F,
                    resources.displayMetrics
                )
            )

            mTextColorBuilder = TextColorBuilder(this, it, mTextStyleable)
            it.recycle()
        }

        if (mTextColorBuilder.isTextGradientColors() || mTextColorBuilder.hasTextStrokeColor()) {
            text = text
        }
        //mTextColorBuilder.intoTextColor()

    }

    private fun drawLine(canvas: Canvas) {
        if (lineWidth <= 0F) return
        //设置下划线颜色
        linePaint.color = lineColor
        linePaint.strokeWidth = lineWidth

        val startX = 0F
        val startY = (height / 2).toFloat()
        val stopX = width.toFloat()
        canvas.drawLine(
            startX,
            startY,
            stopX, startY, linePaint
        )
    }

    /**
     * 显示带有动画效果的数字
     *
     * @param number
     * @param regex
     */
    fun showNumber(number: Float, regex: String = FLOAT_REGEX) {
        //默认为整数
        this.regex = regex
        //修改number属性，会调用setNumber方法
        val objectAnimator = ObjectAnimator.ofFloat(this, "fNumber", fNumber, number)
        objectAnimator.duration = duration
        //加速器，从慢到快到再到慢
        objectAnimator.interpolator = AccelerateDecelerateInterpolator()
        objectAnimator.start()
    }

    fun showNumber(number: String, regex: String = INT_REGEX) {
        //默认为整数
        showNumber(number.toFloat(), regex)
    }

}


/*if (it.isTextGradientColors() || it.isTextStrokeColor()) {
               text = text
               it.intoTextColor()
           }*/

//    //防止下划线高度大到一定值时会覆盖掉文字，需从写此方法
//    override fun setPadding(left: Int, top: Int, right: Int, bottom: Int) {
//        super.setPadding(left, top, right, bottom + lineWidth.toInt())
//    }